package org.jeecg.modules.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.NumberUtil;
import org.jeecg.modules.erp.base.entity.ErpGoodsFormulaDetail;
import org.jeecg.modules.erp.base.entity.ErpGoodsFormulaGs;
import org.jeecg.modules.erp.base.mapper.ErpGoodsFormulaDetailMapper;
import org.jeecg.modules.erp.base.mapper.ErpGoodsFormulaGsMapper;
import org.jeecg.modules.erp.base.service.IErpGoodsFormulaDetailService;
import org.jeecg.modules.erp.base.vo.ErpGoodsFormulaDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Description: 配方明细
 * @Author: jeecg-boot
 * @Date:   2024-05-23
 * @Version: V1.0
 */
@Service
public class ErpGoodsFormulaDetailServiceImpl extends ServiceImpl<ErpGoodsFormulaDetailMapper, ErpGoodsFormulaDetail> implements IErpGoodsFormulaDetailService {

    @Autowired
    private ErpGoodsFormulaGsMapper gsMapper;

    @Override
    public void saveBatchList(List<ErpGoodsFormulaDetail> erpGoodsFormulaDetail) {
        this.saveBatch(erpGoodsFormulaDetail);
    }

    @Override
    public List<ErpGoodsFormulaDetail> listByFormualCode(String formualCode) {
        LambdaQueryWrapper<ErpGoodsFormulaDetail> lambdaQueryChainWrapper = new LambdaQueryWrapper<>();
        return this.list(lambdaQueryChainWrapper.eq(ErpGoodsFormulaDetail::getFormulaCode, formualCode));
    }

    @Override
    public void deleteByFormualCode(String formualCode) {
        this.remove(new LambdaQueryWrapper<ErpGoodsFormulaDetail>().eq(ErpGoodsFormulaDetail::getFormulaCode, formualCode));
    }

    /**
     * 根据配方公式计算数量
     * @param detail
     */
    @Override
    public BigDecimal computeFormula(ErpGoodsFormulaDetailVo vo) {
        ErpGoodsFormulaDetail detail = vo.getDetail();
        String formula = detail.getFormula();
        String materielId = detail.getMaterielId();
        ErpGoodsFormulaGs erpGoodsFormulaGs = gsMapper.selectOne(new LambdaQueryWrapper<ErpGoodsFormulaGs>()
                .eq(ErpGoodsFormulaGs::getMaterialId, materielId));
        String gs = erpGoodsFormulaGs.getGs();
        String gsMaterialId = erpGoodsFormulaGs.getGsMaterialId();
        String[] gsMaterialIds = gsMaterialId.split(",");
        gs = gs.replace("[配方比]", formula);
        List<ErpGoodsFormulaDetail> byMaterielIdsErpGoodsFormulaDetailList = this.baseMapper.findByMaterielIdsErpGoodsFormulaDetailList(Arrays.asList(gsMaterialIds));
        List<String> actualQuantityList = byMaterielIdsErpGoodsFormulaDetailList.stream().map(item -> item.getActualQuantity()).collect(Collectors.toList());
        String computeGs = this.replaceFormual(gs, vo.getList());
        System.out.println(computeGs);
        return NumberUtil.computeByStr(computeGs, 7);
    }


    private String replaceFormual(String content,List<ErpGoodsFormulaDetail> detaillist) {
        String output = content;
        Pattern pattern = Pattern.compile("\\[.*?\\]");
        Matcher matcher = pattern.matcher(output);
        int i =0;
        while (matcher.find()) {
            String match = matcher.group();
            String mat = match.substring(1, match.length() - 1);
            Optional<ErpGoodsFormulaDetail> optional = detaillist.stream().filter(item -> item.getMaterielName().equals(mat)).findFirst();
            String actualQuantity = optional.get().getActualQuantity();
            // 这里可以添加自定义的替换逻辑
//            output = output.replace(match, replaceStr.get(i));
            output = output.replace(match, actualQuantity);
            // 重新匹配替换后的字符串
            matcher = pattern.matcher(output);
            i++;
        }
        return output;
    }

}
